package com.lyb.spzx.product.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyb.spzx.model.dto.h5.ProductSkuDto;
import com.lyb.spzx.model.dto.product.SkuSaleDto;
import com.lyb.spzx.model.entity.product.Product;
import com.lyb.spzx.model.entity.product.ProductDetails;
import com.lyb.spzx.model.entity.product.ProductSku;
import com.lyb.spzx.model.vo.h5.ProductItemVo;
import com.lyb.spzx.product.mapper.ProductDetailMapper;
import com.lyb.spzx.product.mapper.ProductMapper;
import com.lyb.spzx.product.mapper.ProductSkuMapper;
import com.lyb.spzx.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductSkuMapper productSkuMapper;

    @Autowired
    ProductDetailMapper productDetailMapper;

    @Override
    public List<ProductSku> findAllProductSku() {
        List<ProductSku> productSkuList =  productMapper.findAllSku();
        return productSkuList;

    }

    @Override
    public PageInfo<ProductSku> findAllProduct(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSku> productSkus = productSkuMapper.findProductSku(productSkuDto);
        return new PageInfo<>(productSkus);
    }

    @Override
    public ProductItemVo findItem(Long skuId) {
        //根据skuId查询Sku表
        ProductSku productSku = productSkuMapper.findById(skuId);
        //根据productSku的外键ProductId查询product表
        Product product = productMapper.findProductById(productSku.getProductId());
        //根据productSku的外键ProductId查询product_detail表
        ProductDetails productDetails = productDetailMapper.findDetailsByProductId(productSku.getProductId());
        //获取商品详情图
        String imageUrls = productDetails.getImageUrls();
        //按“，”分割成数组
        String[] split = imageUrls.split(",");
        //转换成List集合
        List<String> imageUrlsArray = Arrays.asList(split);

        //获取商品规格
        String specValue = product.getSpecValue();
        //转换成JSONArray格式
        JSONArray specValueJson = JSONArray.parseArray(specValue);

        //获取轮播图
        String sliderUrls = product.getSliderUrls();
        String[] sliderUrlsArr = sliderUrls.split(",");
        List<String> sliderUrlsList = Arrays.asList(sliderUrlsArr);

        //同一个sku规格与skuId对应关系
        List<ProductSku> productSkus = productSkuMapper.findByProductId(productSku.getProductId());
        Map<String, Object> skuSpecValueMap = new HashMap<>();
        productSkus.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(),item.getId());
        });

        ProductItemVo itemVo = new ProductItemVo();
        itemVo.setProduct(product);
        itemVo.setProductSku(productSku);
        itemVo.setDetailsImageUrlList(imageUrlsArray);
        itemVo.setSpecValueList(specValueJson);
        itemVo.setSliderUrlList(sliderUrlsList);
        itemVo.setSkuSpecValueMap(skuSpecValueMap);
        return itemVo;
    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        return productMapper.getBySkuId(skuId);
    }

    @Override
    public Boolean updateSkuSaleNum(List<SkuSaleDto> skuSaleDtoList) {

        if (!CollectionUtils.isEmpty(skuSaleDtoList)){
            for (SkuSaleDto skuSaleDto : skuSaleDtoList) {
                //更新销量（增加）和库存（减少）的值
                productSkuMapper.updateSkuSaleNum(skuSaleDto.getSkuId(),skuSaleDto.getNum());
            }
        }
        return true;
    }
}
